<?php

class Sc_Db_Comment extends Sc_Db_Persistent
{
    protected $_name = 'comment';

    protected $_primary = 'Id';

    protected $_instanceClass = 'Sc_Comment';


	public function getComment($vn, $jp, $username)
    {
//        $where['Isvalid = ?']   = true;
//
//        return $this->getAdapter();
        $select = $this->select();
        $select->from($this);
        $select->setIntegrityCheck(false);
        $select->joinLeft(array("users"), "comment.UserId = users.Id",
        					array("comment.*", "users.Username"));

        $select->where('comment.Isvalid = ?', true);

        if (isset($vn)) {
        	$select->where('VN like ?', '%' . $vn . '%');
        }
    	if (isset($jp)) {
        	$select->where('JP like ?', '%' . $jp . '%');
        }
    	if (isset($username)) {
        	$select->where('users.Username = ?', $username);
        }
        $select->order('Id DESC');

        return $this->fetchall($select)->toArray();
    }

    public function getCommentById($id)
    {
    	$where['Id = ?']   = $id;

        return $this->get($where);
    }

    public function updateComment($comment)
    {
    	$data['VN'] = $comment->vn;
    	$data['JP'] = $comment->jp;
    	$data['DateUpdated'] = new Zend_Db_Expr('CURRENT_TIMESTAMP');

    	$where['Id = ?'] = $comment->id;

    	$ret = $this->update($data, $where);

    	return $ret;
    }

    public function deleteComment($comment)
    {
    	$data['Isvalid'] = 0;
    	$data['DateUpdated'] = new Zend_Db_Expr('CURRENT_TIMESTAMP');

    	$where['Id = ?'] = $comment->id;

    	$ret = $this->update($data, $where);

    	return $ret;
    }

	protected function getConditionForUpdate($record)
    {
        $ret['Id = ?'] = $record->id;
        return $ret;
    }
}